<template>
  <div class="home">
      <home-header></home-header>
      <home-swiper :list="swiperList"></home-swiper>
      <home-icons :list="iconList"></home-icons>
      <home-recommend :list="recommendList"></home-recommend>
      <home-weekend :list="weekendList"></home-weekend>

  </div>
</template>

<script>
// @ is an alias to /src
import HomeHeader from '../components/Header'
import HomeSwiper from '../components/Swiper'
import HomeIcons from '../components/Icons'
import HomeRecommend from '../components/Recommend'
import HomeWeekend from '../components/Weekend'
import {mapState} from 'vuex'


export default {
  name: 'home',
  components: {
      HomeHeader,
      HomeSwiper,
      HomeIcons,
      HomeRecommend,
      HomeWeekend
  },
  data () {
    return {
      swiperList: [],
      iconList: [],
      recommendList:[],
      weekendList:[],
      lastCity:''
    }
  },
   computed:{
     ...mapState(['city'])
   },
  methods:{
    getHomeInfo () {
      this.axios.get('/index.json?city=' + this.city)/*city的值为vuex中储存的city值*/
        .then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc (res) {
      res=res.data;
      if (res.ret && res.data) {//后端正确的返回了结果而且有data值
        const data=res.data;
        this.swiperList=data.swiperList;
        this.iconList=data.iconList;
        this.recommendList=data.recommendList;
        this.weekendList=data.weekendList;
      }
    }
  },
  mounted () {
      this.lastCity=this.city;
      this.getHomeInfo()
  },
   activated() {/*当页面被重新显示时一定会执行一次*/
        if (this.lastCity !== this.city) {
           this.lastCity=this.city;
           this.getHomeInfo()
        }
   }
}
</script>
<style></style>